define(function(require, exports, module) {
    var jQuery = require("jquery");
    (function($) {
        var Notification = function(element, options) {
            // Element collection
            this.$element = $(element);
            this.$note = $('<div class="msg alert"></div>');
            this.options = $.extend(true, {}, $.fn.notify.defaults, options);

            // Setup from options
            if (this.options.transition)
                if (this.options.transition == 'fade')
                    this.$note.addClass('in').addClass(this.options.transition);
                else this.$note.addClass(this.options.transition);
                else this.$note.addClass('fade').addClass('in');

            if (this.options.type)
                this.$note.addClass('alert-' + this.options.type);
            else this.$note.addClass('alert-success');

            if (!this.options.message && this.$element.data("message") !== '') // dom text
                this.$note.html(this.$element.data("message"));
            else
            if (typeof this.options.message === 'object')
                if (this.options.message.html)
                    this.$note.html(this.options.message.html);
                else if (this.options.message.text)
                this.$note.text(this.options.message.text);
            else
                this.$note.html(this.options.message);

            if (this.options.closable)
                var link = $('<a class="close pull-right" href="javascript:;">&times;</a>');
            $(link).on('click', $.proxy(onClose, this));
            this.$note.prepend(link);

            return this;
        };

        var onClose = function() { 
            this.options.onClose&&this.options.onClose();
            $(this.$note).remove();
            this.options.onClosed();
            return false;
        };

        Notification.prototype.show = function() {
            if (this.options.fadeOut.enabled)
                this.$note.delay(this.options.fadeOut.delay || 3000).fadeOut('slow', $.proxy(onClose, this));
            this.$element.empty(); //清空html
            this.$element.append(this.$note);
            this.$note.alert();
        };

        Notification.prototype.hide = function() {
            if (this.options.fadeOut.enabled)
                this.$note.delay(this.options.fadeOut.delay || 3000).fadeOut('slow', $.proxy(onClose, this));
            else onClose.call(this);
        };
        Notification.prototype.close = function() {
            onClose.call(this);
        };

        $.fn.notify = function(options) {
            return new Notification(this, options);
        }; 
        $.fn.notify.defaults = {
            type: 'success',
            closable: false,
            transition: 'fade',
            fadeOut: {
                enabled: true,
                delay: 2000
            },
            message: null,
            onClose: function() {},
            onClosed: function() {}
        }
    })(window.jQuery);
});
